home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / pdcurs21.zip / PORTABLE.ZIP / WGETSTR.C < prev    next >
Text File  |  1992-11-21  |  4KB  |  136 lines

  1. #include <string.h>
  2. #define        CURSES_LIBRARY  1
  3. #include <curses.h>
  4. #undef wgetstr
  5.  
  6. #ifndef        NDEBUG
  7. char *rcsid_wgetstr = "$Header: c:/curses/portable/RCS/wgetstr.c%v 2.0 1992/11/15 03:29:26 MH Rel $";
  8. #endif
  9.  
  10.  
  11.  
  12.  
  13. /*man-start*********************************************************************
  14.  
  15.   wgetstr()    - read string
  16.  
  17.   X/Open Description:
  18.        A series of characters are read until a newline or carriage
  19.        return is received.  The resulting value is placed in the area
  20.        pointed to by the character pointer str.  The user's erase and
  21.        kill characters are interpreted.
  22.  
  23.        NOTE: getstr(), mvgetstr(), and mvwgetstr() are macros.
  24.  
  25.   PDCurses Description:
  26.        The largest string returned will be 255 characters long.
  27.        This can be altered in the library be changing the MAXLINE #define.
  28.  
  29.        WARNING:  This routine does not interpret the user's ERASE and
  30.                  KILL characters as advertised.
  31.  
  32.   X/Open Return Value:
  33.        These functions return OK on success and ERR on error.
  34.  
  35.   PDCurses Errors:
  36.        It is an error to call this function with a NULL window pointer.
  37.  
  38.   Portability:
  39.        PDCurses        int wgetstr( WINDOW* win, char* str );
  40.        X/Open Dec '88  int wgetstr( WINDOW* win, char* str );
  41.        BSD Curses      int wgetstr( WINDOW* win, char* str );
  42.        SYS V Curses    int wgetstr( WINDOW* win, char* str );
  43.  
  44. **man-end**********************************************************************/
  45.  
  46.  
  47. #define MAXLINE 255
  48.  
  49. int    wgetstr(WINDOW *win, char *str)
  50. {
  51.        char    tmp[MAXLINE+1];
  52.        char    txt[MAXLINE+1];
  53.        char    new_char[2] = {0};
  54.        int     ch;
  55.        int     chars = strlen(str);
  56.        int     sy = win->_cury;
  57.        int     sx = win->_curx;
  58.        WINDOW* w;
  59.        bool    oldecho;
  60.        bool    oldcbreak;
  61.        bool    oldnodelay;
  62.  
  63.        if (win == (WINDOW *)NULL)
  64.                return (ERR);
  65.  
  66.        w               = win;
  67.        c_strbeg        = txt;                  /* save for backspacing */
  68.        oldcbreak       = _cursvar.cbreak;      /* remember states       */
  69.        oldecho         = _cursvar.echo;
  70.        oldnodelay      = w->_nodelay;
  71.        _cursvar.echo   = FALSE;                /* we do echo ourselves */
  72.        w->_nodelay     = FALSE;                /* don't return -1       */
  73.  
  74.        strcpy(txt, str);
  75.        wprintw(w, str);
  76.        wrefresh(w);
  77.        ch = wgetch(w);
  78.        while (ch != '\n')
  79.        {
  80.  
  81.                new_char[0] = (ch & CHR_MSK);
  82.                strcat(txt, new_char);
  83.                chars++;
  84.  
  85.                switch (ch)
  86.                {
  87.                case 0x1b:      /* Escape */    /* Terminate String */
  88.                case '\r':      /* CTRL-M */
  89.                case KEY_ENTER:
  90.                        chars--;
  91.                        ch = '\n';
  92.                        continue;
  93.  
  94.                case 0x04:      /* CTRL-D */    /* Delete character */
  95.                case 0x08:      /* CTRL-H */
  96.                        if (chars > 0)
  97.                                PDC_backchar(w, txt, &chars);
  98.                        break;
  99.  
  100.                case 0x17:      /* CTRL-W */    /* Delete word */
  101.                        chars--;
  102.                        while ((txt[chars] == ' ') && (chars > 0))
  103.                                PDC_backchar(w, txt, &chars);
  104.  
  105.                        while ((txt[chars] != ' ') && (chars > 0))
  106.                                PDC_backchar(w, txt, &chars);
  107.                        break;
  108.  
  109.                case 0x15:      /* CTRL-U */    /* Delete line */
  110.                case 0x18:      /* CTRL-X */
  111.                        chars++;
  112.                        while (chars > 0)
  113.                                PDC_backchar(w, txt, &chars);
  114.                        break;
  115.  
  116.                default:
  117.                        waddch(w, ch);
  118.                        wrefresh(w);
  119.                        break;
  120.                }
  121.                ch = wgetch(w);
  122.        }
  123.  
  124.        memset(str, '\0', MAXLINE+1);
  125. #ifdef FLEXOS
  126.        _split_plane(w, txt, tmp, sy, sx, w->_cury, w->_curx - 1);
  127. #endif
  128.        if (strlen(txt) > 0);
  129.        strcpy(str, txt);
  130.  
  131.        _cursvar.echo = oldecho;
  132.        _cursvar.cbreak = oldcbreak;
  133.        win->_nodelay = oldnodelay;
  134.        return (OK);
  135. }
  136.